package com.juzipi.demo.service;


import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

/**
 * 自定义的access方法，主要做了解，不使用
 */

@Service
public class MyUserService {


    public Boolean hasPermission(HttpServletRequest request, Authentication authentication){
        //获取主体
        Object principal = authentication.getPrincipal();
        //判断是否处于UserDetails
        if (principal instanceof UserDetails){
            User user = (User) principal;
            System.out.println(user.getAuthorities());
            System.out.println(user.getUsername());
            //获取权限
            UserDetails userDetails = (UserDetails) principal;
            Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
            //判断请求的uri是否在权限里
            return authorities.contains(new SimpleGrantedAuthority(request.getRequestURI()));


        }

        return false;
    }



}
